﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using System.Data;
using QXBook.Utils;
using QXBook.Controls.HtmlControls;
using QXBook.Service;
using System.Web.UI.HtmlControls;

namespace Web.SysManage.Admin
{
    public partial class AdminList : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(8);

            if (!Page.IsPostBack)
            {
                this.TextBind();
            }

            lmc.AddEditLinkUrl = "AdminEdit.aspx";
            lmc.EnableLinkPublish = false;
            lmc.EnableLinkOn = false;
            lmc.EnableLinkOff = false;
            lmc.EnableLinkShipments = false;
            lmc.EnableLinkResave = false;

            int iRoleId = CommonUtils.ToInt(CommonUtils.Get("RoleID", CheckGetEnum.Safety));
            if (iRoleId == -1)
            {
                lmc.EnableLinkNew = false;
                lmc.EnableLinkResave = true;
            }
            lmc.EditHandler += new EventHandler(lmc_EditHandler);
            lmc.ReSaveHandler += new EventHandler(lmc_ReSaveHandler);
            lmc.DeleteHandler += new EventHandler(lmc_DeleteHandler);
        }

        void lmc_EditHandler(object sender, EventArgs e)
        {
            int flags = 0;
            int memberID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    memberID = CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                Response.Redirect("AdminEdit.aspx?MemberID=" + memberID);
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一个");
            }
            else
            {
                base.ResponseErch("请选择一个管理员");
            }
        }

        void lmc_ReSaveHandler(object sender, EventArgs e)
        {
            int flags = 0;
            string admins = string.Empty;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    admins += hidID.Value + ",";
                    flags++;
                }
            }

            if (flags > 0)
            {
                ServiceFactory.CreateInstance<AdminService>().ReSaveAdmin(base.adminInfo.AdminName, admins.TrimEnd(','));
                base.ResponseErch("恢复成功");
                this.TextBind();
            }
            else
            {
                base.ResponseErch("请选择一个或者多个");
            }
        }

        void lmc_DeleteHandler(object sender, EventArgs e)
        {
            int flags = 0;
            string admins = string.Empty;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    admins += hidID.Value + ",";
                    flags++;
                }
            }

            if (flags > 0)
            {
                ServiceFactory.CreateInstance<AdminService>().DelAdmins(base.adminInfo.AdminName, admins.TrimEnd(','), QXBook.Model.AdminDeleteMod.Logic);
                base.ResponseErch("删除成功");
                this.TextBind();
            }
            else
            {
                base.ResponseErch("请选择一个或者多个");
            }
        }

        protected void TextBind()
        {
            int iRoleId = CommonUtils.ToInt(CommonUtils.Get("RoleID", CheckGetEnum.Safety));

            DataSet ds = new DataSet();
            string strRolename = string.Empty;
            int admincount = 0;
            int rolecount = 0;

            int rtn = ServiceFactory.CreateInstance<AdminService>().GetAdminList(iRoleId, ref admincount, ref rolecount, ref strRolename, ref ds);
            if (rtn < 0)
            {
                return;
            }

            this.sAdmincount.Text = admincount.ToString();
            this.sRolecount.Text = rolecount.ToString();
            this.rptList.DataSource = ds;
            this.rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView Row = (DataRowView)e.Item.DataItem;
            HtmlInputHidden hidID = (HtmlInputHidden)e.Item.FindControl("hidID");
            Anchor aLoginName = (Anchor)e.Item.FindControl("aLoginName");
            Span sNickName = (Span)e.Item.FindControl("sNickName");
            Anchor aAdminRole = (Anchor)e.Item.FindControl("aAdminRole");
            Span sUpdateDate = (Span)e.Item.FindControl("sUpdateDate");
            Img imgLock = (Img)e.Item.FindControl("imgLock");

            hidID.Value = Row["MemberID"].ToString();
            aLoginName.Text = Row["AdminName"].ToString();
            if ((YesNo)CommonUtils.ToInt(Row["IsLock"]) == YesNo.Yes)
            {
                imgLock.Src = "../images/AdminLock.gif";
            }
            else
            {
                imgLock.Src = "../images/AdminUnLock.gif";
            }
            aLoginName.Href = "AdminEdit.aspx?MemberID=" + CommonUtils.UrlEncode(Row["MemberID"].ToString());
            sNickName.Text = Row["NickName"].ToString();
            aAdminRole.Text = Row["RoleName"].ToString();
            aAdminRole.Href = "AdminRoleEdit.aspx?RoleID=" + CommonUtils.UrlEncode(Row["RoleID"].ToString());
            sUpdateDate.Text = !string.IsNullOrEmpty(CommonUtils.ToString(Row["UpdateDate"])) ?
                ((DateTime)Row["UpdateDate"]).ToString("yyyy-MM-dd")
                : string.Empty;
        }
    }
}